package com.example.bbgds.test.home.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.bbgds.test.R;

/**
 * Created by zjzhu on 26/4/16.
 */
public class FloatWindowService extends Service {

    private ImageView mVoiceButton;
    private WindowManager mWindowManager;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        showView();
    }

    private void showView() {
        mVoiceButton = new ImageView(getApplicationContext());
        mVoiceButton.setImageResource(R.drawable.btn_voice);
        mVoiceButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "click voice", Toast.LENGTH_SHORT).show();
            }
        });
        mWindowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        WindowManager.LayoutParams param = new WindowManager.LayoutParams();
        param.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        param.format = PixelFormat.RGBA_8888;
        param.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        param.flags = param.flags | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
        param.flags = param.flags | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
        param.gravity = Gravity.LEFT | Gravity.BOTTOM;
        param.width = WindowManager.LayoutParams.WRAP_CONTENT;
        param.height = WindowManager.LayoutParams.WRAP_CONTENT;
        mWindowManager.addView(mVoiceButton, param);
        Log.e("zhu","sssss");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mVoiceButton != null) {
            mWindowManager.removeView(mVoiceButton);
        }
    }
}
